#################################
# BASIC Tenliners Contest 2017  #
#                               #
# iNVERSO64                     #
# (c) 2017 Roman Werner @romwer #
# email: roman.werner@gmail.com #
#################################

iNVERSO is a fun little picture matching puzzle game with a literal twist.
At the first moment the picture on the right may look completely different form the one on the left but it will "turn" out eventually to be exactly the same. Your eyes and your brain have to work together to wisely choose the next inverse action. Let's give it a go...

Task and Goal
=============
- The goal for each tileset on the right is to match the tileset on the left.
- When you are successfull the border goes green and you can move on to the next puzzle.
- Every new puzzle adds 1 more inverse action to a row or column.
- If you manage to solve the puzzle in the same amount of turns as the displayed puzzle # then call yourself a Pro.
- There is no time limit and no limit in turns. Just have fun working it out.
- Included in the original game are 3 different pictures. If you want to add more pictures or use your own icon designs then why don't you look out for the iNVERSO64 Icon Maker and build your own? Challenge yourself with your own creations.

Keyboard Control:
=================
<CRSR right> = Choose the desired Column
<CRSR down>  = Choose the desired Row
<space>      = Invert all tiles in the chosen Column (or Row)

Please Note:
- The arrows are only moving right (or down) - in a rotation scheme.
- The inverse action remembers the last direction you pressed. To change the inverse action from row to column or vice versa you first have to press the other direction.
- When the tilesets match and the border goes green then press <space> again to move on to the next puzzle.
- After pressing <space> please allow the game a moment to rebuild the tileset.

Game Requirements:
==================
- C64/C128* with BASIC 2.0 runtime environment (or compatible)
- 1 Player

*Also playable on Windows/MacOS/iOS/Android (C64 Emulator required)

Starting in VICE: C64 emulator (http://vice-emu.sourceforge.net/):
==================================================================
Start the emulator and then drag and drop the "inverso64.prg" into the VICE window.
-or-
Load the program with the command: load "inverso64.prg",8
Then start the program with:       run


iNVERSO64 BASIC program code:
=============================
0v=53280:c=1:pOv+1,6:t$="{reverse on}O{cm y}{down}{left}{left}{cm g} {up}{light blue}":s$(0)="{black}"+t$:s$(1)="{red}"+t$:x$=" {down}{left} {down}{left}M{down}{left}N"
1q=q+1:pOv,6:y$="  MN":h$="{home}{down}{down}":?"{clear}{down}{light blue}  puzzle"c"{down}":fOy=0to7:i=128:rEd:fOx=0to7
2m(x,y)=(daNi)/i:i=i/2:?tA2)s$(m(x,y));:n(x,y)=m(x,y):nE:?:?:nE:ifq=3tHreS:q=0
3fOi=1toc:x=rN(0)*8:y=rN(0)*8:z=rN(0)*2+1:onzgoS8,9:nE:c=c+1:x=7:y=7:z=0:s=1724
4fOb=0to7:fOa=0to7:?tA22)s$(n(a,b));:g=g-(m(a,b)=n(a,b)):nE:?:?:nE:ifg=64tHpOv,5
5gEa$:on-(a$="{right}")gO6:on-(a$="{down}")gO7:on-(a$<>" ")gO5:on-(g=64)gO1:onzgoS8,9:gO4
6x=(x+1)aN7:?"{home}{down}"tA20)sPx*2)y$tA36)"  ":z=1:gO5:dA24,20,26,22,114,226,14,28
7y=(y+1)aN7:pOs,32:pOs+40,32:pO214,y*2+1:sY58640:?tA20)x$:z=2:gO5
8x=xaN7:fOa=0to7:n(x,a)=1-n(x,a):nE:g=0:?h$:reT:dA66,189,90,102,194,66,188,196
9y=yaN7:fOa=0to7:n(a,""y)=1-n(a,y):nE:g=0:?h$:reT:dA231,195,129,36,0,195,129,90

Tip: Copy/paste the program-Code into CBM prg Studio (http://www.ajordison.co.uk/).
There you can see the commands in nice color highlighting and you can directly execute the game in an emulator of your choice.


iNVERSO64 Tenliner explained (000=row 0/100=row 1/...):
=======================================================
000 v=53280: rem VIC Frame-Color address
010 c=1: rem Puzzle # counter - starting with 1
020 pokev+1,6: rem set Background-Color to blue
030 t$="{reverse on}O{cm y}{down}{left}{left}{cm g} {up}{light blue}": rem tile
040 s$(0)="{black}"+t$: rem black tile
050 s$(1)="{red}"+t$: rem red tile
060 x$=" {down}{left} {down}{left}M{down}{left}N": rem Arrow pointing right
100 q=q+1: rem picture counter
110 pokev,6: rem set Frame-Color to blue
120 y$="  MN": rem Arrow pointing down
130 h$="{home}{down}{down}": rem re-positioning string
140 print"{clear}{down}{light blue}  puzzle"c"{down}": rem show puzzle #
150 fory=0to7: rem for each line in a picture...
160 i=128: rem (first bit in a byte: 128=%10000000)
170 readd: rem ...read next data byte
180 forx=0to7: rem for each bit in this byte...
200 m(x,y)=(d and i)/i: rem assign set (1) or not set (0) value to the matrix
210 i=i/2: rem prepare next bit (64=%01000000, 32=%00100000, etc.)
220 printtab(2)s$(m(x,y));: rem print black or red tile based on current value
230 n(x,y)=m(x,y): rem also copy this to a second tileset matrix
240 next: rem do this 8 for all bits
250 print: rem tiles are 2 characters wide
260 print: rem therefore print 2 times
270 next: rem do this for all 8 lines
280 ifq=3thenrestore:q=0: rem all 3 pictures/data rows read? Then reset data. 
300 fori=1toc: rem number of row/column inversions = puzzle #
310 x=rnd(0)*8: rem choose random row
320 y=rnd(0)*8: rem choose random column
330 z=rnd(0)*2+1: rem choose either row or column
340 onzgosub800,900: rem inverse row (or column)
350 next: rem do this so many times as the puzzle # dictates
360 c=c+1: rem already prepare next puzzle #
370 x=7: rem Col Position: start with 7 to start with 0 when pressing right
380 y=7: rem Row position: start with 7 to start with 0 when pressing down
390 z=0: rem last direction pressed (0=nothing/1=column/2=row)
395 s=1724: rem Screen position for clearing bottom Arrow pointing right
400 forb=0to7: rem draw inversed tileset - for each row
410 fora=0to7: rem for each tile in row 
420 printtab(22)s$(n(a,b));: rem print black or red tile
430 g=g-(m(a,b)=n(a,b)): rem compare with original matrix. 64 would be a match.
440 next: rem do this for all 8 tiles in this row
450 print: rem tiles are 2 characters wide
460 print: rem therefore print 2 times
470 next: rem do this for all 8 rows
480 ifg=64thenpokev,5: rem Perfect match? Then turn frame color to green.
500 geta$: rem read key
510 on-(a$="{right}")goto600: rem if right key was pressed then move arrow right  
520 on-(a$="{down}")goto700: rem if down key was pressed then move arrow down
530 on-(a$<>" ")goto500: rem if space key was not pressed then read next key
540 on-(g=64)goto100: rem when perfect match then goto next puzzle
550 onzgosub800,900: rem depending on last direction inverse row or column
560 goto400: rem redraw tileset
600 x=(x+1)and7: rem move arrow to next position ('and7' makes value 8 into 0).
610 print"{home}{down}"tab(20)spc(x*2)y$tab(36)"  ": rem print arrow at new pos
620 z=1: rem set direction to column mode
630 goto500: rem check next key
640 data24,20,26,22,114,226,14,28: rem data for first picture (music note)
700 y=(y+1)and7: rem move arrow to next position ('and7' makes value 8 into 0).
710 pokes,32: rem clean up arrow characters of lowest arrow position
720 pokes+40,32: rem same on the next line 40 characters below
730 poke214,y*2+1: rem set new row position (col value in 211 does not matter)
740 sys58640: rem set cursor
750 printtab(20)x$: rem print arrow pointing right
760 z=2: rem set direction to row mode
770 goto500: rem check next key
800 x=xand7: rem make sure x is between 0 and 7
810 fora=0to7: rem for each tile in column
820 n(x,a)=1-n(x,a): rem make 1 to 0 and 0 to 1
830 next: rem do this for all remaining tiles
840 g=0: rem reset tile match total
850 printh$: rem reposition cursor
860 return
870 data66,189,90,102,194,66,188,196: rem data for second picture (pug)
900 y=yand7: rem make sure y is between 0 and 7
910 fora=0to7: rem for each tile in row
920 n(a,y)=1-n(a,y): rem make 1 to 0 and 0 to 1
930 next: rem do this for all remaining tiles
940 g=0: rem reset tile match total
950 printh$: rem reposition cursor
960 return
970 data231,195,129,36,0,195,129,90: rem data for third picture (invader)

Tip: if you like to add more pictures then place more 8 byte data sets after line 970 and increase the picture counter check in line 280